//
//  XJUserAccountViewModel.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/22.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit

class XJUserAccountViewModel: NSObject {
    //路径
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    //全局访问点
    static let sharedTools: XJUserAccountViewModel = XJUserAccountViewModel()
    //创建一个用户模型
    var userAccount: XJUserAccount?
    //计算型属性
    var accessToken: String?{
        //代表用户有accessToken
        if userAccount?.access_token != nil{
            //看是否过期
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return userAccount?.access_token
            }else{
                //代表已经过期
                return nil
            }
        }else{
            //代表没有accesstoken  用户没有登录  或者没有保存数据
            return nil
        }
    }
    var isLogin: Bool {
        return accessToken != nil
    }
//Mark 错误点
    override init() {
        super.init()
        userAccount = getUserAccount()
    }
}

//归档和解档
extension XJUserAccountViewModel{
    func saveUserAccount(userAccount: XJUserAccount){
        //为了解决第一次userAccount为nil的bug
        self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: file)
    }
    func getUserAccount()-> XJUserAccount?{
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? XJUserAccount
        
        return result
    }
}
//请求数据
extension XJUserAccountViewModel {
    
    func loadUserAccount(code :String, isSuccess:((isSucc: Bool)->())){
        
        XJNetworkTools.sharedTools.oauthLoadUserAccount(code, success: { (response) -> () in
            guard let res = response as? [String :AnyObject] else {
                
                isSuccess(isSucc: false)
                
                return
            }
            //字典转模型
            let userAccount = XJUserAccount(dict: res)
            
            self.loadUserInfo(userAccount, isSuccess: isSuccess)
            }) { (error) -> () in
                
                isSuccess(isSucc: false)
                
        }
        //请求用户信息
    }
    
    func loadUserInfo(userAccount: XJUserAccount,isSuccess:((isSucc: Bool)->())){
        
        XJNetworkTools.sharedTools.oauthLoadUserInfo(userAccount, success: { (response) -> () in
            
            guard let res = response as? [String :AnyObject] else {
                
                isSuccess(isSucc: false)
                
                return
            }
            
            // 因为当前的screenname 和avatar_large 本来就是可选值 所以在as的时候 只用as? 转字符串
            userAccount.screen_name = res["screen_name"] as? String
            userAccount.avatar_large = res["avatar_large"] as? String
            
            self.saveUserAccount(userAccount)
            
            isSuccess(isSucc: true)
            
            }) { (error) -> () in
                
                isSuccess(isSucc: false)
        }
    }
}
